package thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

//Callable的使用
public class Demo22 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //Callable只是定义了一个“具有返回值”的任务，但是执行还需要Thread类帮忙
      Callable<Integer> callable = new Callable<Integer>() {
          @Override
          public Integer call() throws Exception {
              int sum = 0;
              for (int i = 1; i <= 100; i++) {
                  sum += i;
              }
              return sum;
          }
      };

      //通过FutureTask接收到返回值
        FutureTask<Integer> futureTask = new FutureTask<>(callable);

        Thread t = new Thread(futureTask);
        t.start();
        //如果线程没有执行完，get也会有阻塞效果
        System.out.println(futureTask.get());
    }
}
